package com.xyh.qzjservice.controller;

import com.xyh.qzjservice.pojo.*;
import com.xyh.qzjservice.service.IQzj;
import com.xyh.qzjservice.service.IUser;
import com.xyh.qzjservice.utils.Convert;
import com.xyh.qzjservice.utils.JSONResult;
import com.xyh.qzjservice.utils.Nulls;
import com.xyh.qzjservice.utils.ValidatorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class QzjController {

    @Autowired
    private IUser iUser;

    @Autowired
    private IQzj iQzj;

    @RequestMapping(value = "/haha", method = RequestMethod.POST)
    public JSONResult haha() {
        return JSONResult.ok(iUser.getAll());
    }

    @RequestMapping(value = "/sdx", method = RequestMethod.POST)
    public JSONResult getSdx() {
        Map map = new HashMap();
        map.put("GRBM", "41010201010104981");
        map.put("YLZH", "ZY06010105011");
        List result = iQzj.getSdx(map);
        return JSONResult.ok(result);
    }

    @RequestMapping(value = "/getPatinfo", method = RequestMethod.POST)
    public JSONResult getPatinfo(@RequestBody getPatinfoParam param) {
        //验证
        try {
            ValidatorUtils.validateEntity(param);
        } catch (Exception e) {
            return JSONResult.errorMsg("表单验证失败！原因：" + e.getMessage());
        }

        Map map = new HashMap();
        map.put("nhcode",param.getNhcode());
        List patinfolist;
        try {
            patinfolist = iQzj.getPatinfo(map);
            if(patinfolist.size() == 0){
                return JSONResult.errorMsg("未查询到病人信息！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.errorMsg(e.getMessage());
        }

        return JSONResult.ok(patinfolist.get(0));
    }

    @RequestMapping(value = "/getBc", method = RequestMethod.POST)
    public JSONResult getBc(@RequestBody getBcParam param) {
        //验证
        try {
            ValidatorUtils.validateEntity(param);
        } catch (Exception e) {
            return JSONResult.errorMsg("表单验证失败！原因：" + e.getMessage());
        }
        List bcList = new ArrayList();
        Map map = new HashMap();
        map.put("zyh",param.getZyh());
        try {
            bcList = iQzj.getBc(map);
            if(bcList.size() == 0){
                bcList = iQzj.getBcHistory(map);
            }
            if(bcList.size() == 0){
                return JSONResult.errorMsg("未查询到该患者的补偿信息！");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSONResult.ok(bcList.get(0));
    }

    @RequestMapping(value = "/cancelBill", method = RequestMethod.POST)
    public JSONResult cancelBill(@RequestBody getBcParam param) {
        //验证
        try {
            ValidatorUtils.validateEntity(param);
        } catch (Exception e) {
            return JSONResult.errorMsg("表单验证失败！原因：" + e.getMessage());
        }
        Map map = new HashMap();
        map.put("hospcode",param.getZyh());
        try {
            iQzj.cancelBill(map);
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.errorMsg(e.getMessage());
        }
        return JSONResult.ok();
    }

    @RequestMapping(value = "/uploadPatinfoNh", method = RequestMethod.POST)
    public JSONResult uploadPatinfoNh(@RequestBody uploadPatinfoNhParam param) {
        //验证
        try {
            ValidatorUtils.validateEntity(param);
        } catch (Exception e) {
            return JSONResult.errorMsg("表单验证失败！原因：" + e.getMessage());
        }
        //取试点县
        String chalknumb = param.getChalknumb();
        String medinumb = param.getMedinumb();
        Map map = new HashMap();
        map.put("chalknumb", chalknumb);
        map.put("medinumb", medinumb);
        List sdxlist = iQzj.getSdx(map);
        if (sdxlist.size() == 0) {
            return JSONResult.errorMsg("未查询到试点县信息");
        }
        String sdx = ((Map) sdxlist.get(0)).get("SDX") + "";

        Map uploadMap = new HashMap();
        uploadMap.put("sdx", sdx);
        uploadMap.put("grbh", param.getGrbh());
        uploadMap.put("hospcode", param.getHospcode());
        uploadMap.put("hospdate", param.getHospdate());
        uploadMap.put("outdate", param.getOutdate());
        uploadMap.put("zyts", Convert.toInteger(param.getZyts()));
        uploadMap.put("officecode", param.getOfficecode());
        uploadMap.put("officename", param.getOfficename());
        uploadMap.put("recidoctname", param.getRecidoctname());
        uploadMap.put("ryqk", param.getRyqk());
        uploadMap.put("hospresu", param.getHospresu());
        uploadMap.put("cyqk", param.getCyqk());
        uploadMap.put("outresu", param.getOutresu());

        try {
            iQzj.uploadPatinfoNh(uploadMap);
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.errorMsg(e.getMessage());
        }
        return JSONResult.ok();
    }

    @RequestMapping(value = "/uploadFymx", method = RequestMethod.POST)
    public JSONResult uploadFymx(@RequestBody List<fymx> params) {
        if (params.size() == 0) {
            return JSONResult.errorMsg("没有需要上传的明细信息！");
        }

        for (int i = 0; i < params.size(); i++) {
            Map paramMap = new HashMap();
            try {
                String hospcode = params.get(i).getHospcode() + "";
                String detailid = params.get(i).getDetailid() + "";
                String sfsj = params.get(i).getSfsj() + "";
                String four = params.get(i).getFour() + "";
                String medicode = params.get(i).getMedicode() + "";
                String dycode = params.get(i).getDycode() + "";
                String mediname = params.get(i).getMediname() + "";
                String spec = params.get(i).getSpec() + "";
                String unitprice = params.get(i).getUnitprice() + "";
                String ambunit = params.get(i).getAmbunit() + "";
                String usernum = params.get(i).getUsernum() + "";
                String price = params.get(i).getPrice() + "";
                String doctid = params.get(i).getDoctid() + "";
                String doctname = params.get(i).getDoctname() + "";
                String thegroup = params.get(i).getThegroup() + "";
                String serial = params.get(i).getSerial() + "";
                String usageid = params.get(i).getUsageid() + "";
                String theoquan = params.get(i).getTheoquan() + "";
                String unit = params.get(i).getUnit() + "";
                String timeid = params.get(i).getTimeid() + "";
                String lzts = params.get(i).getLzts() + "";
                String marktype = params.get(i).getMarktype() + "";
                String ydetailid = params.get(i).getYdetailid() + "";

                paramMap.put("hospcode", Nulls.nullToString(hospcode));
                paramMap.put("detailid",Nulls.nullToString(detailid));
                paramMap.put("sfsj",Nulls.nullToString(sfsj));
                paramMap.put("four",Nulls.nullToLong(four));
                paramMap.put("medicode",Nulls.nullToString(medicode));
                paramMap.put("dycode",Nulls.nullToString(dycode));
                paramMap.put("mediname",Nulls.nullToString(mediname));
                paramMap.put("spec",Nulls.nullToString(spec));
                paramMap.put("unitprice",Convert.toBigDecimal(unitprice));
                paramMap.put("ambunit",Nulls.nullToString(ambunit));
                paramMap.put("usernum",Convert.toBigDecimal(usernum));
                paramMap.put("price",Convert.toBigDecimal(price));
                paramMap.put("fs",Convert.toBigDecimal("1"));
                paramMap.put("doctid",Nulls.nullToString(doctid));
                paramMap.put("doctname",Nulls.nullToString(doctname));
                paramMap.put("thegroup",Nulls.nullToString(thegroup));
                paramMap.put("serial",Nulls.nullToString(serial));
                paramMap.put("usageid",Nulls.nullToString(usageid));
                paramMap.put("theoquan",Convert.toBigDecimal(theoquan));
                paramMap.put("unit",Nulls.nullToString(unit));
                paramMap.put("timeid",Nulls.nullToString(timeid));
                paramMap.put("lzts",Convert.toBigDecimal(lzts));
                paramMap.put("marktype",Nulls.nullToString(marktype));
                paramMap.put("ydetailid",Nulls.nullToString(ydetailid));
            }catch (Exception e){
                return JSONResult.errorMsg("his内部错误！原因：" + e.getMessage());
            }
            try {
                iQzj.uploadFymx(paramMap);
            } catch (Exception e) {
                e.printStackTrace();
                return JSONResult.errorMsg("向前置机写入项目: " + paramMap.get("mediname") + "时失败！原因：" + e.getMessage());
            }
        }
        return JSONResult.ok();
    }

    @RequestMapping(value = "/cancelUpload", method = RequestMethod.POST)
    public JSONResult cancelUpload(@RequestBody cancelUploadParam param) {
        //验证
        try {
            ValidatorUtils.validateEntity(param);
        } catch (Exception e) {
            return JSONResult.errorMsg("表单验证失败！原因：" + e.getMessage());
        }
        try {
            String zyh = param.getZyh();
            Map deleteMap = new HashMap();
            deleteMap.put("zyh",zyh);
            iQzj.deletefymx(deleteMap);
            iQzj.deletezyjl(deleteMap);
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.errorMsg(e.getMessage());
        }
        return JSONResult.ok();
    }

    @RequestMapping(value = "/uploadPatinfoNhCy", method = RequestMethod.POST)
    public JSONResult uploadPatinfoNhCy(@RequestBody uploadPatinfoNhCyParam param) {
        //验证
        try {
            ValidatorUtils.validateEntity(param);
        } catch (Exception e) {
            return JSONResult.errorMsg("表单验证失败！原因：" + e.getMessage());
        }

        Map uploadMap = new HashMap();
        uploadMap.put("unitefrcode", param.getUnitefrcode());
        uploadMap.put("chalknumb", param.getChalknumb());
        uploadMap.put("hospcode", param.getHospcode());
        uploadMap.put("hospdate", param.getHospdate());
        uploadMap.put("outdate", param.getOutdate());
        uploadMap.put("zyts", Convert.toInteger(param.getZyts()));
        uploadMap.put("officecode", param.getOfficecode());
        uploadMap.put("officename", param.getOfficename());
        uploadMap.put("recidoctname", param.getRecidoctname());
        uploadMap.put("ryqk", param.getRyqk());
        uploadMap.put("hospresu", param.getHospresu());
        uploadMap.put("cyqk", param.getCyqk());
        uploadMap.put("outresu", param.getOutresu());

        try {
            iQzj.uploadPatinfoNhCy(uploadMap);
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.errorMsg(e.getMessage());
        }
        return JSONResult.ok();
    }

    //批量上传在院病人信息
    @RequestMapping(value = "/uploadPatinfoNhNew", method = RequestMethod.POST)
    public JSONResult uploadPatinfoNhNew(@RequestBody List<uploadPatinfoNhNewParam> params) {
        if(params.size() == 0){
            return JSONResult.ok();
        }
        for (int i = 0; i < params.size(); i++) {
            Map paramMap = new HashMap();
            try {
                //取试点县
                String chalknumb = params.get(i).getChalknumb();
                String medinumb = params.get(i).getMedinumb();
                String name = params.get(i).getName();
                Map map = new HashMap();
                map.put("chalknumb", chalknumb);
                map.put("medinumb", medinumb);
                List sdxlist = iQzj.getSdx(map);
                if (sdxlist.size() == 0) {
                    return JSONResult.errorMsg("上传患者：" + name + "时，取试点县信息失败！");
                }
                String sdx = ((Map) sdxlist.get(0)).get("SDX") + "";
                String hospcode = params.get(i).getHospcode();
                String hospdate = params.get(i).getHospdate();
                String outdate = params.get(i).getOutdate();
                String zyts = params.get(i).getZyts();
                String officecode = params.get(i).getOfficecode();
                String officename = params.get(i).getOfficename();
                String recidoctname = params.get(i).getRecidoctname();
                String ryqk = params.get(i).getRyqk();
                String hospresu = params.get(i).getHospresu();
                String cyqk = params.get(i).getCyqk();
                String outresu = params.get(i).getOutresu();
                String recidoct = params.get(i).getRecidoct();
                String sex = params.get(i).getSex();
                String age = params.get(i).getAge();
                String idno = params.get(i).getIdno();
                String jkkh = params.get(i).getJkkh();
                String address = params.get(i).getAddress();
                String telno = params.get(i).getTelno();

                paramMap.put("sdx",sdx);
                paramMap.put("grbh",chalknumb);
                paramMap.put("hospcode",hospcode);
                paramMap.put("hospdate",hospdate);
                paramMap.put("outdate",outdate);
                paramMap.put("zyts",Convert.toInteger(zyts));
                paramMap.put("officecode",officecode);
                paramMap.put("officename",officename);
                paramMap.put("recidoctname",recidoctname);
                paramMap.put("ryqk",ryqk);
                paramMap.put("hospresu",hospresu);
                paramMap.put("cyqk",cyqk);
                paramMap.put("outresu",outresu);
                paramMap.put("recidoct",recidoct);
                paramMap.put("name",name);
                paramMap.put("sex",sex);
                paramMap.put("age",Convert.toInteger(age));
                paramMap.put("idno",idno);
                paramMap.put("medinumb",medinumb);
                paramMap.put("jkkh",jkkh);
                paramMap.put("address",address);
                paramMap.put("telno",telno);
            }catch (Exception e){
                return JSONResult.errorMsg(e.getMessage());
            }
            try {
                iQzj.uploadPatinfoNew(paramMap);
            } catch (Exception e) {
                e.printStackTrace();
                return JSONResult.errorMsg("向前置机写入患者: " + params.get(i).getName() + "时失败！原因：" + e.getMessage());
            }
        }
        return JSONResult.ok();
    }

}
